package com.yunquan.ohana.ui.home.calendar

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.google.gson.Gson
import com.yanzhenjie.recyclerview.SwipeMenuCreator
import com.yanzhenjie.recyclerview.SwipeMenuItem
import com.yanzhenjie.recyclerview.SwipeRecyclerView
import com.yanzhenjie.recyclerview.widget.DefaultItemDecoration
import com.yunquan.ohana.BR
import com.yunquan.ohana.R
import com.yunquan.ohana.base.bus.RxBus
import com.yunquan.ohana.base.ui.BaseFragment
import com.yunquan.ohana.databinding.FragmentCalendarBinding
import com.yunquan.ohana.dialog.TipDialog
import com.yunquan.ohana.entity.EventEntity
import com.yunquan.ohana.entity.EventType
import com.yunquan.ohana.entity.FramesDataManager
import com.yunquan.ohana.entity.FramesEntity
import com.yunquan.ohana.ui.calendar.CalendarMenuActivity
import com.yunquan.ohana.ui.home.frames.adapter.FramesAdapter
import com.yunquan.ohana.ui.home.frames.adapter.FramesType
import com.yunquan.ohana.ui.photos.PhotosActivity
import com.yunquan.ohana.utils.Utils

class CalendarFragment : BaseFragment<FragmentCalendarBinding, CalendarViewModel>() {

    companion object {
        fun newInstance(): CalendarFragment {
            return CalendarFragment()
        }
    }

    private var mFramesList = mutableListOf<FramesEntity>()
    private var mAdapter: FramesAdapter? = null

    override fun initContentView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
    ): Int {
        return R.layout.fragment_calendar
    }

    override fun initVariableId(): Int {
        return BR.calendarViewModel
    }

    override fun initData(savedInstanceState: Bundle?) {
        binding!!.rvFrames.isNestedScrollingEnabled = false
        binding!!.rvFrames.layoutManager = LinearLayoutManager(requireContext())

        binding!!.rvFrames.setSwipeMenuCreator(mSwipeMenuCreator)
        binding!!.rvFrames.setOnItemMenuClickListener { menuBridge, adapterPosition ->
            menuBridge.closeMenu()
            if (menuBridge.direction == SwipeRecyclerView.RIGHT_DIRECTION) {
                showUnbindDialog(mAdapter!!.getItem(adapterPosition))
            }
        }

        mFramesList.addAll(FramesDataManager.instance.getData())
        mAdapter = FramesAdapter(FramesType.CALENDAR)
        mAdapter!!.setOnShareListener { item ->
            val bundle = Bundle()
            bundle.putString("deviceName", item.name)
            bundle.putString("deviceId", item.id)
            startActivity(CalendarMenuActivity::class.java, bundle)
        }
        binding!!.rvFrames.adapter = mAdapter


        mAdapter?.setData(FramesDataManager.instance.getData())
        viewModel!!.notData.set(FramesDataManager.instance.getData().isNotEmpty())
    }

    override fun initViewObservable() {
        binding!!.layoutRefresh.setOnRefreshListener {
            RxBus.getDefault().post(EventEntity(EventType.GET_FRAMES))
        }
        viewModel!!.ui.observe(viewLifecycleOwner) {
            when (it) {
                0 -> {
                    mAdapter?.setData(FramesDataManager.instance.getData())
                }

                1 -> {
                    viewModel!!.notData.set(FramesDataManager.instance.getData().isNotEmpty())
                    binding!!.layoutRefresh.isRefreshing = false
                }
            }
        }
    }

    /**
     * 显示解绑弹窗
     */
    private fun showUnbindDialog(frame: FramesEntity) {
        val dialog = TipDialog(requireContext())
        dialog.setTitle(getString(R.string.unbind_frames))
        dialog.setMessage(getString(R.string.tip_unbind_frames))
        dialog.setOnDialogClickListener(object : TipDialog.OnDialogClickListener() {
            override fun onRightClick() {
                super.onRightClick()
                viewModel!!.unbindFrame(frame)
            }
        })
        dialog.show()
    }

    /**
     * 跳转相册选择
     */
    private fun goToAlbum(frames: List<FramesEntity>) {
        val framesJson = Gson().toJson(frames)
        val bundle = Bundle()
        bundle.putString("frames", framesJson)
        startActivity(PhotosActivity::class.java, bundle)
    }

    // 侧滑菜单
    private val mSwipeMenuCreator = SwipeMenuCreator { _, rightMenu, position ->
        val viewType = mAdapter?.getItemViewType(position)
        if (viewType == 0) {
            val deleteItem = SwipeMenuItem(requireContext())
            deleteItem.setImage(R.mipmap.ic_frames_unbind)
            deleteItem.height = ViewGroup.LayoutParams.MATCH_PARENT
            deleteItem.width = Utils.instance.dp2px(60f)
            rightMenu.addMenuItem(deleteItem)
        }
    }

}